package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.SysWarehouse;
import com.ruoyi.web.controller.demo.domain.SysWarehouseUse;
import com.ruoyi.web.controller.service.ISysWarehouseService;
import com.ruoyi.web.controller.service.ISysWarehouseUseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.InstrumentList;
import com.ruoyi.web.controller.service.IInstrumentListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仪器设备Controller
 * 
 * @author ruoyi
 * @date 2023-01-02
 */
@Controller
@RequestMapping("/system/list")
public class InstrumentListController extends BaseController
{
    private String prefix = "system/list";
    @Autowired
    private ISysWarehouseService sysWarehouseService;
    @Autowired
    private IInstrumentListService instrumentListService;
    @Autowired
    private ISysWarehouseUseService sysWarehouseUseService;
    @RequiresPermissions("system:list:view")
    @GetMapping()
    public String list()
    {
        return prefix + "/list";
    }

    /**
     * 查询仪器设备列表
     */
    @RequiresPermissions("system:list:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InstrumentList instrumentList)
    {
        startPage();
        List<InstrumentList> list = instrumentListService.selectInstrumentListList(instrumentList);
        return getDataTable(list);
    }

    /**
     * 导出仪器设备列表
     */
    @RequiresPermissions("system:list:export")
    @Log(title = "仪器设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InstrumentList instrumentList)
    {
        List<InstrumentList> list = instrumentListService.selectInstrumentListList(instrumentList);
        ExcelUtil<InstrumentList> util = new ExcelUtil<InstrumentList>(InstrumentList.class);
        return util.exportExcel(list, "仪器设备数据");
    }

    /**
     * 新增仪器设备
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        SysUser sysUser = getSysUser();
        List<SysWarehouse> sysWarehouses = sysWarehouseService.selectSysWarehouseList(null);
        model.addAttribute("user",sysUser);
        model.addAttribute("wareHouse",sysWarehouses);
        return prefix + "/add";
    }

    /**
     * 新增保存仪器设备
     */
    @RequiresPermissions("system:list:add")
    @Log(title = "仪器设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InstrumentList instrumentList)
    {
        //根据查找的库房id  查询库房（查询出的是对象）的剩余容量  //一个判断 如果（不到满【0】）就加入且剩余减一 如果剩余的是0，就不能入库
        //查出要加入的库房
        if (instrumentList.getInstrumentOut().equals("未出库")){
            SysWarehouse sysWarehouse = sysWarehouseService.selectSysWarehouseById(instrumentList.getSaveLocation());
            System.out.println(sysWarehouse);
            if (Math.toIntExact(sysWarehouse.getLastLine()) == 0){
                return  AjaxResult.warn("库房已满");
            }else{
                long l = sysWarehouse.getLastLine() - 1;
                if (l == 0){
                    sysWarehouse.setState("已满");
                }
                //更新
                SysWarehouse ware = new SysWarehouse();
                ware.setLastLine(l);
                ware.setId(instrumentList.getSaveLocation());
                sysWarehouseService.updateLine(ware);


                instrumentList.setCreatedTime(new Date());
                instrumentListService.insertInstrumentList(instrumentList);


                SysWarehouseUse sysWarehouseUse = new SysWarehouseUse();
                sysWarehouseUse.setMake("入库");
                sysWarehouseUse.setHouseId(instrumentList.getSaveLocation());
                sysWarehouseUse.setInsId(instrumentList.getInstrumentId());
                return toAjax(sysWarehouseUseService.insertSysWarehouseUse(sysWarehouseUse));
            }
        }
        instrumentList.setCreatedTime(new Date());
        return toAjax(instrumentListService.insertInstrumentList(instrumentList));
    }

    /**
     * 修改仪器设备
     */
    @RequiresPermissions("system:list:edit")
    @GetMapping("/edit/{instrumentId}")
    public String edit(@PathVariable("instrumentId") Long instrumentId, ModelMap mmap,Model model)
    {
        SysUser sysUser = getSysUser();
        model.addAttribute("user",sysUser);
        List<SysWarehouse> sysWarehouses = sysWarehouseService.selectSysWarehouseList(null);
        model.addAttribute("wareHouse",sysWarehouses);
        InstrumentList instrumentList1 = instrumentListService.selectInstrumentListByInstrumentId(instrumentId);
        model.addAttribute("name",instrumentList1);
        InstrumentList instrumentList = instrumentListService.selectInstrumentListByInstrumentId(instrumentId);
        mmap.put("instrumentList", instrumentList);
        return prefix + "/edit";
    }

    /**
     * 修改保存仪器设备
     */
    @RequiresPermissions("system:list:edit")
    @Log(title = "仪器设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InstrumentList instrumentList)
    {
        return toAjax(instrumentListService.updateInstrumentList(instrumentList));
    }

    /**
     * 删除仪器设备
     */
    @RequiresPermissions("system:list:remove")
    @Log(title = "仪器设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(instrumentListService.deleteInstrumentListByInstrumentIds(ids));
    }
}
